package sdmaker.parser;

import java.util.ArrayList;
import java.util.List;

import sdmaker.ast.AST;
import sdmaker.ast.ASTsequence;

import fsa.FSA;
import fsa.State;
import fsa.Transition;

public class SDMakerGenerator {
	private List<ParseResult> program;
	private FSA fsa;


	/* ************************ *
	 * ***** CONSTRUCTORS ***** *
	 * ************************ */

	SDMakerGenerator(FSA fsa){
		program= new ArrayList<ParseResult>();
		this.fsa= fsa;
	}



	/* ************************ *
	 * ******** GETTERS ******* *
	 * ************************ */

	public int getProgramSize(){
		return program.size();
	}

	public ParseResult getLastParsed(){
		ParseResult lastParsed= null;

		if(program.size()>0){
			lastParsed= Utils.getLast(program);
		}

		return lastParsed;
	}

	public SetStateData getFinalState(){
		SetStateData stateDataSet= new SetStateData();

		for(State state : fsa.getFinalStates()){
			stateDataSet.add(StateData.getStateData(state));
		}

		return stateDataSet;
	}



	/* ********************************* *
	 * **** SETTERS/ADDERS/REMOVERS **** *
	 * ********************************* */

	public void add(ParseResult parseResult){
		program.add(parseResult);
	}

	public void remove(ParseResult parseResult){
		for(int i= 0; i < program.size(); i++){
			if(program.get(i)==parseResult){
				program.remove(i);
				break;
			}
		}
	}

	public void removeLast(){
		if(program.size()>0){
			remove(Utils.getLast(program));
		}
	}



	/* ************************ *
	 * ******** OTHERS ******** *
	 * ************************ */

	public AST generateFinalAST(){
		AST finalAST= null;

		if(program.size() > 0){
			finalAST= new ASTsequence(Utils.toListAST(program));
		}

		return finalAST;
	}
}
